package com.lagou.web;

import com.lagou.domain.Student;
import com.lagou.service.AddService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Date;
import java.sql.SQLException;

public class AddServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String stuno = req.getParameter("stuno");
        String stuname = req.getParameter("stuname");
        String stusex = req.getParameter("stusex");
        String stubir = req.getParameter("stubir");

        Student stu = new Student();
        stu.setStuno(Integer.parseInt(stuno));
        stu.setStuname(stuname);
        stu.setStusex(stusex);
        stu.setStubir(Date.valueOf(stubir));

        AddService addService = new AddService();
        try {
            int count = addService.add(stu);
            if(count>0){
                resp.getWriter().println("添加成功！");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
